<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<%@ page import="kr.co.insoft.framework.config.Authorities"%>
<c:set var="CONTEXT">${pageContext.request.contextPath}</c:set>
<c:set var="cssUrl">${pageContext.request.contextPath}/resources/css</c:set>
<c:set var="jsUrl">${pageContext.request.contextPath}/resources/js</c:set>
<div class="col-sm-12 page-header custom-page-header">
	<h1><spring:message code="menu.comparison.dashboard"/></h1>
	<small><spring:message code="menu.comparison.dashboard.desc"/></small>
</div>
<!-- 차트 컨테이너 -->
<div class="chartGroup col-sm-6">
	<div id="PieChartContainer">
	</div>
</div>
<div class="chartGroup col-sm-6">
	<div id="BarChartContainer">
	</div>
</div>

<script type="text/javascript">
$(document).ready(function() {
	/* 조회 */ 
	getChartData();
});	
 
function getChartData() {

	$.post("./chart", null , function(res) {
	}).fail(function() {
		bootbox.alert("<spring:message code='common.server.fail'/>");
	}).done(function(res) {
		if(res){
			drawPieChart(new Object(res.pie));
			drawBarChart(new Object(res.bar));
		}
	}).always(function() {

	});
}

function drawPieChart(dataList) {
    var options = {
            chart : {
                renderTo: 'PieChartContainer',
                plotBackgroundColor: null,
                plotBorderWidth: null,
                plotShadow: false
            },
            tooltip: {
                formatter: function() {
                      return this.point.name +'<br>'+ (this.percentage).toFixed(1) + '%' ;
                }
            },
            title: {
                text: '',
                x: 0,
                y: 20,
                layout: 'vertical',
                align: 'left',
                verticalAlign: 'top'
            },                
            plotOptions: {
                pie: {
                    allowPointSelect: true,
                    cursor: 'pointer',
                    dataLabels: {
                        enabled: true,
                        format: '<b>{point.name}</b>: {point.percentage:.1f} %',
                        style: {
                            color: (Highcharts.theme && Highcharts.theme.contrastTextColor) || 'black'
                        }
                    }
                }
            },
            plotOptions : {
                pie: {
                    allowPointSelect: true,
                    cursor: 'pointer',
                    dataLabels: {
                        enabled: true,
                        color: '#000000',
                        connectorColor: '#000000',
                        formatter: function() {
                            return  this.point.name + '<br>' + this.point.y.toFixed(2) + '$';
                        }
                    },
                    showInLegend: true
                }
            },
            series: []
        };
    var seriese = [];
    
    $.each(dataList,
    function(i, item) {
        var test = [];
        test.push(this.venderName, parseFloat(this.cost));
        seriese.push(test);
    });    
    
    chart = new Highcharts.Chart(options); 
    chart.addSeries({
        type : 'pie',
        name :'test',
        data : seriese
    });
    chart.redraw();       
}
 
function drawBarChart(dataList) {
    var options = {
            chart: {
                type: 'column',
                renderTo: 'BarChartContainer',
                marginRight: 100,
                marginTop:10
            },
            title: {
                text: '',
                x: 0,
                y: 0,
                layout: 'vertical',
                align: 'left',
                verticalAlign: 'top'
            }, 
            xAxis: {
                categories: []
            },
            yAxis: {
                min: 0,
                title: {
                    text: 'Cost ($)'
                }
            },

            tooltip: {
                formatter: function() {
                    return '<b>'+ this.x +'</b><br/>'+
                        this.series.name +': '+ this.y +'$<br/>';
                },
				horizonAlign : 'center'
            },
            plotOptions: {
                column: {
                    pointPadding: 0.2,
                    borderWidth: 0
                }
            },
            series: []
        };     
    

    $.each(dataList, function(i, item) {
        var awsSeries = {
        	    data : []
        };
        awsSeries.name = this.venderName;
        awsSeries.data.push( parseFloat(this.lastMonthCost), parseFloat(this.thisMonthCost));
        options.series.push(awsSeries);
    });
    options.xAxis.categories.push(dataList[0].lastMonth);  
    options.xAxis.categories.push(dataList[0].thisMonth);

    chart = new Highcharts.Chart(options);       
}
 </script>
